#!/bin/sh

cleanup() {
  unset -f grep >/dev/null 2>&1 || :
  unset GREP_OPTIONS
  [ -z "${ORIG_PATH-}" ] || PATH="${ORIG_PATH}"
  case "${TEST_DIR-}" in /*) rm -rf "${TEST_DIR}" ;; esac
}
die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

unset GREP_OPTIONS

TEST_DIR="$(mktemp -d)" || die 'could not create a temp dir'

# stdin: matching lines are printed and the status is 0
OUTPUT="$(printf 'foo\nbar\nfoobar\n' | nvm_grep foo)"
EXIT_CODE=$?
[ "_${OUTPUT}" = "_foo
foobar" ] || die "matching stdin: got >${OUTPUT}<, expected >foo\\nfoobar<"
[ "_${EXIT_CODE}" = "_0" ] || die "matching stdin: got exit >${EXIT_CODE}<, expected >0<"

# no match: no output and status 1
OUTPUT="$(printf 'foo\n' | nvm_grep baz)"
EXIT_CODE=$?
[ "_${OUTPUT}" = "_" ] || die "no match: got >${OUTPUT}<, expected ><"
[ "_${EXIT_CODE}" = "_1" ] || die "no match: got exit >${EXIT_CODE}<, expected >1<"

# an unreadable file: status 2, error on stderr only
OUTPUT="$(nvm_grep foo "${TEST_DIR}/missing" 2>/dev/null)"
EXIT_CODE=$?
[ "_${OUTPUT}" = "_" ] || die "missing file: got stdout >${OUTPUT}<, expected ><"
[ "_${EXIT_CODE}" = "_2" ] || die "missing file: got exit >${EXIT_CODE}<, expected >2<"
ERR="$(nvm_grep foo "${TEST_DIR}/missing" 2>&1 >/dev/null)"
[ -n "${ERR}" ] || die 'missing file: expected an error on stderr'

# options and file operands are passed through
printf 'Alpha\nbeta\nalpha beta\n' > "${TEST_DIR}/file" || die 'could not write file'
OUTPUT="$(nvm_grep -i -c alpha "${TEST_DIR}/file")"
[ "_${OUTPUT}" = "_2" ] || die "-i -c: got >${OUTPUT}<, expected >2<"

OUTPUT="$(nvm_grep -v -e alpha "${TEST_DIR}/file")"
[ "_${OUTPUT}" = "_Alpha
beta" ] || die "-v -e: got >${OUTPUT}<, expected >Alpha\\nbeta<"

# a pattern with a space stays one argument
OUTPUT="$(nvm_grep 'alpha beta' "${TEST_DIR}/file")"
[ "_${OUTPUT}" = "_alpha beta" ] || die "spaced pattern: got >${OUTPUT}<, expected >alpha beta<"

# a pattern starting with a dash, via -e
OUTPUT="$(printf 'a\n-x\n' | nvm_grep -e '-x')"
[ "_${OUTPUT}" = "_-x" ] || die "dash pattern: got >${OUTPUT}<, expected >-x<"

# -q: no output, status reflects the match
OUTPUT="$(nvm_grep -q beta "${TEST_DIR}/file")"
EXIT_CODE=$?
[ "_${OUTPUT}" = "_" ] || die "-q match: got >${OUTPUT}<, expected ><"
[ "_${EXIT_CODE}" = "_0" ] || die "-q match: got exit >${EXIT_CODE}<, expected >0<"
nvm_grep -q gamma "${TEST_DIR}/file" && die '-q with no match returned 0'

# a `grep` shell function is bypassed
grep() { echo 'grep function called'; return 0; }
OUTPUT="$(printf 'foo\n' | nvm_grep baz)"
EXIT_CODE=$?
[ "_${OUTPUT}" = "_" ] || die "grep function: got >${OUTPUT}<, expected ><"
[ "_${EXIT_CODE}" = "_1" ] || die "grep function: got exit >${EXIT_CODE}<, expected >1<"
unset -f grep

# grep sees no GREP_OPTIONS, and all arguments arrive intact
mkdir "${TEST_DIR}/bin" || die 'could not create bin dir'
cat > "${TEST_DIR}/bin/grep" <<'SCRIPT'
#!/bin/sh
echo ">${GREP_OPTIONS:-}<"
for arg in "$@"; do
  echo "[${arg}]"
done
exit 7
SCRIPT
chmod +x "${TEST_DIR}/bin/grep" || die 'could not chmod fake grep'

ORIG_PATH="${PATH}"
PATH="${TEST_DIR}/bin:${PATH}"
GREP_OPTIONS='--invert-match'
export GREP_OPTIONS
OUTPUT="$(nvm_grep -e 'a b' '' '*')"
EXIT_CODE=$?
EXPECTED='><
[-e]
[a b]
[]
[*]'
[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "fake grep: got >${OUTPUT}<, expected >${EXPECTED}<"
[ "_${EXIT_CODE}" = "_7" ] || die "fake grep: got exit >${EXIT_CODE}<, expected >7<"

# the caller's GREP_OPTIONS is left alone (direct call, not a subshell)
nvm_grep x >/dev/null
[ "_${GREP_OPTIONS}" = "_--invert-match" ] || die "caller GREP_OPTIONS: got >${GREP_OPTIONS}<, expected >--invert-match<"

cleanup
